//
//  CertifyViewController.m
//  MyNewProject
//
//  Created by 王春辉 on 2019/12/26.
//  Copyright © 2019年 sun. All rights reserved.
//

#import "CertifyViewController.h"
#import "CQCountDownButton.h"
#import "YQNetworking.h"
#import "WJPhotoManager.h"

#pragma mark -- mark 数据相关
#import "WJBaseModel.h"
#import "UserInfoModel.h"

@interface CertifyViewController ()<UITextFieldDelegate> {
    UITextField *_nameTextField;
    UITextField *_idCardTextField;
    UITextField *_phoneNumTextField;
    UITextField *_verifyTextField;
}
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *realNameView;
@property (nonatomic, strong) UIView *idNumView;
@property (nonatomic, strong) UIView *phoneNumView;
@property (nonatomic, strong) UIView *verifyView;
@property (nonatomic, strong) UIView *picsView;
@property (nonatomic, strong) UIButton *commitBtn;
@property (nonatomic, strong) CQCountDownButton *countButton;

@property (nonatomic, copy) NSString *realName; // 真实姓名
@property (nonatomic, copy) NSString *idCardNum; // 身份证号
@property (nonatomic, copy) NSString *phoneNum; // 手机号
@property (nonatomic, copy) NSString *verifyNum; // 验证码
@property (nonatomic, strong) UIImage *frontImage; // 正面图片
@property (nonatomic, strong) UIImage *backImage; // 背面
@property (nonatomic, strong) UIImage *catchImage; // 手持图片

@property (nonatomic, strong) UIView *alertView; // 成功弹框

@end

@implementation CertifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.translatesAutoresizingMaskIntoConstraints = true;
    UIButton *skipButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [skipButton setTitle:@"跳过" forState:UIControlStateNormal];
    skipButton.titleLabel.font = SKFONT_SIZE(13);
    [skipButton setTitleColor:SKColorWithHex(0x333333) forState:UIControlStateNormal];
    [skipButton addTarget:self action:@selector(skipButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:skipButton];
    self.gk_navRightBarButtonItem = rightBarButtonItem;
    self.gk_navTitle = @"实名认证";
    self.gk_backStyle = GKNavigationBarBackStyleBlack;
    [self initContentViews];
    
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keboardDismiss:)];
    [self.scrollView addGestureRecognizer:tapGesture];
}

-(void)keboardDismiss:(UIGestureRecognizer*)gesture {
    [self.view endEditing:YES];
}

- (void)initContentViews {
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.top.mas_offset(SKTopBar_Height);
        make.bottom.mas_offset(-SKBottomSafe_Height);
    }];
    
    [self.realNameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(8);
        make.left.right.mas_offset(0);
        make.width.mas_equalTo(self.view);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(49.0/667);
    }];
    
    [self.idNumView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.width.height.mas_equalTo(self.realNameView);
        make.top.mas_equalTo(self.realNameView.mas_bottom).mas_offset(0.5);
    }];
    
    [self.phoneNumView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.width.height.mas_equalTo(self.realNameView);
        make.top.mas_equalTo(self.idNumView.mas_bottom).mas_offset(0.5);
    }];
    
    [self.verifyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.width.height.mas_equalTo(self.realNameView);
        make.top.mas_equalTo(self.phoneNumView.mas_bottom).mas_offset(0.5);
    }];
    
    [self.picsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.realNameView);
        make.top.mas_equalTo(self.verifyView.mas_bottom).mas_offset(8);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(292.5/667);
    }];
    
    [self.commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.mas_equalTo(self.picsView.mas_bottom).mas_offset(26);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(40.0/667);
        make.bottom.mas_offset(-26);
    }];
    
}

#pragma mark -- mark 懒加载
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        self.scrollView = [[UIScrollView alloc] init];
        _scrollView.showsVerticalScrollIndicator = false;
        _scrollView.showsHorizontalScrollIndicator = false;
        _scrollView.backgroundColor = SKColorWithHex(0xF7F7F7);
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField isEqual:_nameTextField]) {
        self.realName = textField.text;
    }
    if ([textField isEqual:_phoneNumTextField]) {
        self.phoneNum = textField.text;
    }
    if ([textField isEqual:_verifyTextField]) {
        self.verifyNum = textField.text;
    }
    if ([textField isEqual:_idCardTextField]) {
        self.idCardNum = textField.text;
    }
}
- (UIView *)realNameView {
    if (!_realNameView) {
        _realNameView = [[UIView alloc] init];
        _realNameView.backgroundColor = [UIColor whiteColor];
        UILabel *titleLabel = [self getTitleLabelWith:@"真实姓名"];
        [_realNameView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.width.mas_equalTo(60);
            make.centerY.mas_offset(0);
        }];
        
        UITextField *textField = [self getTextFieldWith:@"请填写姓名"];
        textField.delegate = self;
        NSString *name = [UserInfoSingleton shareInstance].userInfo.name;
        if (name) {
            textField.text = name;
            self.realName = name;
        }
        _nameTextField = textField;
        [_realNameView addSubview:textField];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(titleLabel.mas_right).mas_offset(15);
            make.top.mas_offset(2);
            make.bottom.mas_offset(-2);
            make.right.mas_offset(-15);
        }];
        [self.scrollView addSubview:_realNameView];
    }
    return _realNameView;
}
- (NSString *)phoneNum {
    if (!_phoneNum) {
        _phoneNum = [UserInfoSingleton shareInstance].userInfo.mobile;
    }
    return _phoneNum;
}
- (UIView *)idNumView {
    if (!_idNumView) {
        _idNumView = [[UIView alloc] init];
        _idNumView.backgroundColor = [UIColor whiteColor];
        UILabel *titleLabel = [self getTitleLabelWith:@"身份证号"];
        [_idNumView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.width.mas_equalTo(60);
            make.centerY.mas_offset(0);
        }];
        
        UITextField *textField = [self getTextFieldWith:@"请输入身份证号"];
        textField.delegate = self;
        NSString *idCard = [UserInfoSingleton shareInstance].userInfo.card;
        if (idCard) {
            textField.text = idCard;
            self.idCardNum = idCard;
        }
        _idCardTextField = textField;
        [_idNumView addSubview:textField];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(titleLabel.mas_right).mas_offset(15);
            make.top.mas_offset(2);
            make.bottom.mas_offset(-2);
            make.right.mas_offset(-15);
        }];
        [self.scrollView addSubview:_idNumView];
    }
    return _idNumView;
}
- (UIView *)phoneNumView {
    if (!_phoneNumView) {
        _phoneNumView = [[UIView alloc] init];
        _phoneNumView.backgroundColor = [UIColor whiteColor];
        UILabel *titleLabel = [self getTitleLabelWith:@"电话号码"];
        [_phoneNumView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.width.mas_equalTo(60);
            make.centerY.mas_offset(0);
        }];
        
        UITextField *textField = [self getTextFieldWith:@"请输入电话号码"];
        textField.text = [UserInfoSingleton shareInstance].userInfo.mobile;
        textField.enabled = false;
        textField.delegate = self;
        _phoneNumTextField = textField;
        [_phoneNumView addSubview:textField];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(titleLabel.mas_right).mas_offset(15);
            make.top.mas_offset(2);
            make.bottom.mas_offset(-2);
            make.right.mas_offset(-15);
        }];
        [self.scrollView addSubview:_phoneNumView];
    }
    return _phoneNumView;
}
- (UIView *)verifyView {
    if (!_verifyView) {
        _verifyView = [[UIView alloc] init];
        _verifyView.backgroundColor = [UIColor whiteColor];
        UILabel *titleLabel = [self getTitleLabelWith:@"验证码"];
        [_verifyView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.width.mas_equalTo(60);
            make.centerY.mas_offset(0);
        }];
        
        UITextField *textField = [self getTextFieldWith:@"请输入短信验证码"];
        textField.delegate = self;
        _verifyTextField = textField;
        [_verifyView addSubview:textField];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(titleLabel.mas_right).mas_offset(15);
            make.top.mas_offset(2);
            make.bottom.mas_offset(-2);
        }];
        [_verifyView addSubview:self.countButton];
        [self.countButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-15);
            make.top.mas_offset(4);
            make.bottom.mas_offset(-4);
        }];
        [self.scrollView addSubview:_verifyView];
    }
    return _verifyView;
}

- (UIView *)picsView {
    if (!_picsView) {
        _picsView = [[UIView alloc] init];
        _picsView.backgroundColor = [UIColor whiteColor];
        UIImageView *imageView1 = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(@"guide_id_card_front_img")];
        [_picsView addSubview:imageView1];
        UIImageView *imageView2 = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(@"guide_id_card_back_img")];
        [_picsView addSubview:imageView2];
        UIImageView *imageView3 = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(@"guide_id_card_catch_img")];
        [_picsView addSubview:imageView3];
        UIImageView *imageView4 = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(@"guide_id_card_catch_img")];
        [_picsView addSubview:imageView4];
        
        imageView1.tag = 5200;
        imageView2.tag = 5201;
        imageView3.tag = 5202;
        
        UILabel *label1 = [[UILabel alloc] init];
        label1.textAlignment = NSTextAlignmentCenter;
        label1.text = @"请上传身份证正面";
        label1.font = SKFONT_Medium(12);
        label1.textColor = SKColorWithHex(0x333333);
        [_picsView addSubview:label1];
        UILabel *label2 = [[UILabel alloc] init];
        label2.text = @"请上传身份证反面";
        label2.font = SKFONT_Medium(12);
        label2.textColor = SKColorWithHex(0x333333);
        label2.textAlignment = NSTextAlignmentCenter;
        [_picsView addSubview:label2];
        UILabel *label3 = [[UILabel alloc] init];
        label3.text = @"上上传手持身份证照片";
        label3.textAlignment = NSTextAlignmentCenter;
        label3.font = SKFONT_Medium(12);
        label3.textColor = SKColorWithHex(0x333333);
        [_picsView addSubview:label3];
        UILabel *label4 = [[UILabel alloc] init];
        label4.text = @"示例";
        label4.textAlignment = NSTextAlignmentCenter;
        label4.font = SKFONT_Medium(12);
        label4.textColor = SKColorWithHex(0x333333);
        [_picsView addSubview:label4];
        
        UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn1.tag = 521;
        [btn1 addTarget:self action:@selector(takePhotosButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn1 setImage:SKIMAGE_NAMED(@"guide_id_btn_img") forState:UIControlStateNormal];
        [_picsView addSubview:btn1];
        
        UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn2.tag = 522;
        [btn2 addTarget:self action:@selector(takePhotosButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn2 setImage:SKIMAGE_NAMED(@"guide_id_btn_img") forState:UIControlStateNormal];
        [_picsView addSubview:btn2];
        
        UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn3.tag = 523;
        [btn3 addTarget:self action:@selector(takePhotosButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn3 setImage:SKIMAGE_NAMED(@"guide_id_btn_img") forState:UIControlStateNormal];
        [_picsView addSubview:btn3];
        
        
        [imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.top.mas_offset(20);
            make.right.mas_equalTo(imageView2.mas_left).mas_offset(-38);
            make.bottom.mas_equalTo(label1.mas_top).mas_offset(-10);
            make.size.mas_equalTo(imageView2);
            make.size.mas_equalTo(imageView3);
            make.size.mas_equalTo(imageView4);
        }];
    
        [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(imageView1);
        }];
        
        [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.mas_equalTo(imageView1);
            make.right.mas_offset(-15);
        }];
        
        [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(imageView2);
        }];
        
        [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(imageView1);
            make.height.with.mas_equalTo(label2);
            make.height.with.mas_equalTo(label3);
            make.height.with.mas_equalTo(label4);
        }];
        
        [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(imageView2);
            make.top.bottom.mas_equalTo(label1);
        }];
        
        [imageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(imageView1);
            make.top.mas_equalTo(label1.mas_bottom).mas_offset(10);
        }];
        
        [btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(imageView3);
        }];
        
        [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(imageView3);
            make.top.mas_equalTo(imageView3.mas_bottom).mas_offset(10);
            make.bottom.mas_offset(-10);
        }];
        
        [imageView4 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(imageView2);
            make.top.bottom.mas_equalTo(imageView3);
        }];
        
        [label4 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(imageView4);
            make.top.bottom.mas_equalTo(label3);
        }];
        
        [self.scrollView addSubview:_picsView];
    }
    return _picsView;
}

- (UIButton *)commitBtn {
    if (!_commitBtn) {
        _commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_commitBtn setTitle:@"提交" forState:UIControlStateNormal];
        [_commitBtn addTarget:self action:@selector(commitButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _commitBtn.backgroundColor = SKColorWithHex(0xE33030);
        _commitBtn.titleLabel.font = SKFONT_Medium(16);
        [_commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _commitBtn.layer.masksToBounds = true;
        _commitBtn.layer.cornerRadius = 4.0f;
        [self.scrollView addSubview:_commitBtn];
    }
    return _commitBtn;
}
- (CQCountDownButton *)countButton {
    if (!_countButton) {
        __weak typeof(self) weakSelf = self;
        self.countButton = [[CQCountDownButton alloc] initWithDuration:10 buttonClicked:^{
            [SVProgressHUD showWithStatus:@"正在获取验证码..."];
            [YQNetworking postWithUrl:@"Login/sendsms" refreshRequest:YES cache:NO params:@{@"mobile":weakSelf.phoneNum,@"sign":@"yupinyouxuan*****yupinyouxuan"} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            } successBlock:^(id response) {
                NSDictionary *dic = (NSDictionary*)response;
                if ([dic[@"status"] isEqualToString:@"ok"])
                {
                    [SVProgressHUD showSuccessWithStatus:@"验证码已发送"];
                    [weakSelf.countButton startCountDown];
                }
                else
                {
                    [SVProgressHUD showErrorWithStatus:@"获取失败，请重试"];
                    weakSelf.countButton.enabled = YES;
                }
            } failBlock:^(NSError *error) {
                
            }];
        } countDownStart:^{
            //------- 倒计时开始 -------//
            NSLog(@"倒计时开始");
        } countDownUnderway:^(NSInteger restCountDownNum) {
            //------- 倒计时进行中 -------//
            [weakSelf.countButton setTitle:[NSString stringWithFormat:@"重新获取(%ld)", restCountDownNum] forState:UIControlStateNormal];
        } countDownCompletion:^{
            //------- 倒计时结束 -------//
            [weakSelf.countButton setTitle:@"获取验证码" forState:UIControlStateNormal];
            NSLog(@"倒计时结束");
        }];
        
        [_countButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_countButton setTitleColor:SKColorWithHex(0x4444444) forState:UIControlStateNormal];
        [_countButton setTitleColor:SKColorWithHex(0x999999) forState:UIControlStateDisabled];
        _countButton.titleLabel.font = SKFONT_Medium(15);
    }
    return _countButton;
}

- (UILabel *)getTitleLabelWith:(NSString *)title {
    UILabel *label = [[UILabel alloc] init];
    label.text = title;
    label.textColor = SKColorWithHex(0x333333);
    label.font = SKFONT_Medium(15);
    label.textAlignment = NSTextAlignmentLeft;
    return label;
}
- (UITextField *)getTextFieldWith:(NSString *)placeHolder {
    UITextField *textField = [[UITextField alloc] init];
    textField.placeholder = placeHolder;
    textField.font = SKFONT_Medium(14);
    textField.textColor = SKColorWithHex(0x999999);
    return textField;
}


- (UIView *)alertView {
    if (!_alertView) {
        _alertView = [[UIView alloc] init];
        _alertView.backgroundColor = SKRGBAColor(0, 0, 0, 0.5);
        [[UIApplication sharedApplication].keyWindow addSubview:_alertView];
        [self.alertView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
        UIView *alert = [[UIView alloc] init];
        alert.backgroundColor = [UIColor whiteColor];
        alert.layer.masksToBounds = true;
        alert.layer.cornerRadius = 4.0;
        [_alertView addSubview:alert];
        [alert mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_offset(0);
            make.width.mas_equalTo(self.view.mas_width).multipliedBy(235.0/375);
            make.height.mas_equalTo(alert.mas_width).multipliedBy(260.0/235);
        }];
        
        UIButton *okBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [okBtn setTitle:@"我知道了" forState:UIControlStateNormal];
        [okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        okBtn.backgroundColor = SKColorWithHex(0xE33030);
        okBtn.titleLabel.font = SKFONT_SIZE(17);
        okBtn.layer.masksToBounds = true;
        okBtn.layer.cornerRadius = 4.0;
        [okBtn addTarget:self action:@selector(okIknowButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [alert addSubview:okBtn];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(@"guide_id_card_verify_success_img")];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [alert addSubview:imageView];
        
        UILabel *label1 = [[UILabel alloc] init];
        label1.textColor = SKColorWithHex(0x111111);
        label1.text = @"恭喜您，认证成功";
        label1.font = SKFONT_SIZE(17);
        [alert addSubview:label1];
        
        UILabel *label2 = [[UILabel alloc] init];
        label2.textColor = SKColorWithHex(0x666666);
        label2.font = SKFONT_SIZE(13);
        label2.text = @"现在就去跟随大牛一起赚钱吧";
        [alert addSubview:label2];
        
        [okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(-25);
            make.width.mas_equalTo(alert.mas_width).multipliedBy(150.0/235);
            make.height.mas_equalTo(okBtn.mas_width).multipliedBy(40.0/150);
            make.centerX.mas_offset(0);
        }];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_offset(0);
            make.top.mas_offset(25);
            make.size.mas_equalTo(CGSizeMake(80, 80));
        }];
        
        [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(imageView.mas_bottom).mas_offset(10);
            make.centerX.mas_offset(0);
            make.bottom.mas_equalTo(label2.mas_top);
            make.height.mas_equalTo(label2);
        }];
        [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(okBtn.mas_top).mas_offset(-10);
            make.centerX.mas_offset(0);
        }];
    }
    return _alertView;
}


#pragma mark -- mark 业务逻辑
- (void)setFrontImage:(UIImage *)frontImage {
    _frontImage = frontImage;
    UIImageView *imageView = [self.picsView viewWithTag:5200];
    imageView.image = frontImage;
}

- (void)setBackImage:(UIImage *)backImage {
    _backImage = backImage;
    UIImageView *imageView = [self.picsView viewWithTag:5201];
    imageView.image = backImage;
}

- (void)setCatchImage:(UIImage *)catchImage {
    _catchImage = catchImage;
    UIImageView *imageView = [self.picsView viewWithTag:5202];
    imageView.image = catchImage;
}

/**
 认证成功
 */
- (void)showCertifySuccessAlert {
    self.alertView.hidden = false;
}

#pragma mark -- mark 网络请求

- (void)setupUserIdCard {
    if (!(self.uid && self.loginKey)) {
        [self showMessage:@"请重新登录"];
        return; }
    NSDictionary *param = @{@"uid":@([self.uid intValue]),
                            @"loginKey":self.loginKey,
                            @"name":self.realName,
                            @"card":self.idCardNum,
                            @"code":self.verifyNum};
    NSArray <NSData *>*datas = @[UIImageJPEGRepresentation(_frontImage, 0.1),
                                 UIImageJPEGRepresentation(_backImage, 0.1),
                                 UIImageJPEGRepresentation(_catchImage, 0.1)];
    NSArray <NSString *>*names = @[@"idcard_img_up",
                                   @"idcard_img_down",
                                   @"idcard_img_hand"];
    SKWeakSelf;
    [YQNetworking updateFilesWithUrl:@"my/setCard" parameters:param fileDatas:datas type:@"file.jpg" names:names mimeType:@"imge/jpeg" progressBlock:^(int64_t bytesWritten, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        WJBaseModel *model = [WJBaseModel modelWithDictionary:response];
        [weakSelf showMessage:model.msg];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if ([model isSuccess]) {
                // 认证成功
                // 刷新用户信息
                // 返回
                [weakSelf certifySuccess];
            }else {
                
            }
        });
    } failBlock:^(NSError *error) {
        [weakSelf debugShowMessage:error.localizedDescription];
    }];
}

/**
 刷新用户信息
 */
- (void)refreshUserInfoComplete:(void(^)(BOOL))complete {
    SKWeakSelf;
    [YQNetworking getWithUrl:@"my/getUserInfo" params:@{@"uid":self.uid, @"loginKey":self.loginKey} reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        UserInfoModel *model = [UserInfoModel modelWithDictionary:response];
        if ([model isSuccess]) {
            YPLoginUserInfo *info = [YPLoginUserInfo modelObjectWithDictionary: model.user_info.modelToJSONObject];
            [UserInfoSingleton shareInstance].userInfo = info;
            SKUserUserDefaults_Save(response[@"user_info"] , @"UserInfo");
        }else {
            [weakSelf showMessage:model.msg];
        }
        if (complete) {
            complete([model isSuccess]);
        }
    } failBlock:^(NSError *error) {
        [weakSelf debugShowMessage:error.localizedDescription];
    }];
}

/**
 认证成功
 */
- (void)certifySuccess {
    // 认证成功
    // 刷新用户信息
    // 返回
    SKWeakSelf;
    [self refreshUserInfoComplete:^(BOOL success) {
        if (success) {
            // 认证成功
            if (weakSelf.certifyResultBlock) {
                weakSelf.certifyResultBlock(success);
            }
            [weakSelf.navigationController popViewControllerAnimated:true];
        }
    }];
}

#pragma mark -- mark EVENT

/**
 照相按钮点击事件

 @param sender sender
 */
- (void)takePhotosButtonClick:(UIButton *)sender {
    SKWeakSelf;
    [[WJPhotoManager manager] choosePhotoWithViewController:self AndSize:CGSizeMake(MIN(self.view.width, self.view.height), MIN(self.view.width, self.view.height) / 154.0 * 97.0) Complete:^(UIImage * _Nonnull image) {
        if (sender.tag == 521) {
            // 正面
            weakSelf.frontImage = image;
        }else if (sender.tag == 522) {
            // 反面
            weakSelf.backImage = image;
        }else if (sender.tag == 523) {
            // 手持
            weakSelf.catchImage = image;
        }else {}
    }];
}

/**
 button点击 跳过

 @param sender sender
 */
- (void)skipButtonClick:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:true];
}

/**
 提交信息

 @param sender sender
 */
- (void)commitButtonClick:(UIButton *)sender {
    if (!(self.realName && self.idCardNum && self.backImage && self.catchImage && self.frontImage && self.phoneNum && self.verifyNum)) {
        // 数据缺失
        [self showMessage:@"请检查信息是否录入完整"];
        return; }
    [self setupUserIdCard];
}

/**
 ok 我知道了

 @param sender sender
 */
- (void)okIknowButtonClick:(UIButton *)sender {
    self.alertView.hidden = true;
}
@end
